<?php

class Azio_Posts
{

    public function __construct()
    {

    }

    public static function cut_tring_by_char($string, $max_length)
    {
        if (mb_strlen($string, "UTF-8") > $max_length) {
            $max_length = $max_length - 3;
            $string = mb_substr($string, 0, $max_length, "UTF-8");
            $pos = strrpos($string, " ");
            if ($pos === false) {
                return substr($string, 0, $max_length) . "...";
            }
            return substr($string, 0, $pos) . "...";
        } else {
            return $string;
        }
    }

    /*
     * get Category Parent
     * $taxonomy : category | timeline | section (string)
     */

    public function getCategories($taxonomy = "category")
    {
        $cacheId = __CLASS__ . __METHOD__ . $taxonomy;
        $result = wp_cache_get($cacheId);
        if (false === $result) {
            $args = array(
                'type' => 'post',
                'parent' => 0,
                'orderby' => 'name',
                'order' => 'ASC',
                'taxonomy' => $taxonomy,
                'pad_counts' => false
            );
            $result = get_categories($args);
            wp_cache_set($cacheId, $result, 'default', self::CACHETIME); // cache 1 hour
        }
        return $result;
    }

    /*
     * get Category By Parent
     * $category : category id (int)
     * $taxonomy : category | timeline | section (string)
     */

    public function getCategoriesByParent($category, $taxonomy = "category")
    {
        $cacheId = __CLASS__ . __METHOD__ . $category . $taxonomy;
        $result = wp_cache_get($cacheId);
        if (false === $result) {
            $args = array(
                'type' => 'post',
                'child_of' => $category,
                'orderby' => 'name',
                'order' => 'ASC',
                'taxonomy' => $taxonomy
            );
            $result = get_categories($args);
            foreach ($result as &$value) {
                $value->permalink = get_term_link(intval($value->term_id), $taxonomy);
            }
            wp_cache_set($cacheId, $result, 'default', self::CACHETIME); // cache 1 hour
        }
        return $result;
    }

    /*
     * Get all posts
     * 
     * @param int $limit number of posts to be shown
     * @param int $category category of post
     * @param string $meta_key
     * @param string $meta_value
     * @param array $post_not_in array of posts not be shown
     * @param string $post_type type of post (post|timeline|tool)
     * @param string $orderby order posts by
     */

    public static function getPosts($limit, $category = "", $meta_key = "", $meta_value = "", $post_not_in = "", $post_type = "post", $orderby = "post_date")
    {
        //$cacheId = __CLASS__ . __METHOD__ . $limit . $category . $meta_key . $meta_value . $post_not_in . $orderby . $post_type;
        //$result = wp_cache_get($cacheId);
        //if (false === $result) {
        $args = array(
            'posts_per_page' => $limit,
            'orderby' => $orderby,
            'order' => 'DESC',
            'post_type' => $post_type,
            'post_status' => 'publish',
        );

        if (!empty($category)) {
            $args['category'] = $category;
        }

        if (!empty($meta_key)) {
            $args['meta_key'] = $meta_key;
            $args['meta_value'] = $meta_value;
        }

        if (!empty($post_not_in)) {
            $args['post__not_in'] = $post_not_in;
        }

        $objects = get_posts($args);

        foreach ($objects as $object) {
            $object->filter = 'sample';
            $permalink = get_permalink($object);
            $object->permalink = $permalink;

            $thumbnail_id = get_post_thumbnail_id($object->ID);
            $image = wp_get_attachment_image_src($thumbnail_id, 'thumbnail', true);
            $object->image_thumbnail = $image[0];

            $image = wp_get_attachment_image_src($thumbnail_id, 'featured-image', true);
            $object->featured_image = $image[0];

            $image = wp_get_attachment_image_src($thumbnail_id, 'avatar', true);
            $object->image_avatar = $image[0];

            $object->date = get_the_time('d/m/Y', $object->ID);

            if($object->post_excerpt !== ''){
                $object->excerpt = $object->post_excerpt;
            }else{
                $object->excerpt = Azio_Posts::cut_tring_by_char(strip_tags($object->post_content), 250);
            }


            $result[] = $object;
        }

        //wp_cache_set($cacheId, $result, 'default', self::CACHETIME); // cache 1 hour
        //}
        return $result;
    }

    public static function getRelatedSeafoods($limit, $term_id, $post_not_in)
    {
        $args = array(
            'posts_per_page' => $limit,
            'orderby' => 'rand',
            'post_type' => 'seafood',
            'post_status' => 'publish',
            'post__not_in' => $post_not_in,
            'tax_query' => array(
                array(
                    'taxonomy' => 'seafoodtype',
                    'field' => 'id',
                    'terms' => $term_id
                )
            )
        );


        $objects = get_posts($args);

        foreach ($objects as $object) {
            $object->filter = 'sample';
            $permalink = get_permalink($object);
            $object->permalink = $permalink;

            $thumbnail_id = get_post_thumbnail_id($object->ID);
            $image = wp_get_attachment_image_src($thumbnail_id, 'thumbnail', true);
            $object->image_thumbnail = $image[0];

            $image = wp_get_attachment_image_src($thumbnail_id, 'medium', true);
            $object->image_medium = $image[0];

            $image = wp_get_attachment_image_src($thumbnail_id, 'large', true);
            $object->image_large = $image[0];

            $result[] = $object;
        }

        return $result;
    }

    public static function getSeafoods($limit, $meta_key = "", $meta_value = "", $orderby = "post_date")
    {

        $args = array(
            'posts_per_page' => $limit,
            'orderby' => $orderby,
            'order' => 'DESC',
            'post_type' => 'seafood',
            'post_status' => 'publish'
        );

        if (!empty($meta_key)) {
            $args['meta_key'] = $meta_key;
            $args['meta_value'] = $meta_value;
        }

        $objects = get_posts($args);

        foreach ($objects as $object) {
            $object->filter = 'sample';
            $permalink = get_permalink($object);
            $object->permalink = $permalink;

            $thumbnail_id = get_post_thumbnail_id($object->ID);
            $image = wp_get_attachment_image_src($thumbnail_id, 'thumbnail', true);
            $object->image_thumbnail = $image[0];

            $image = wp_get_attachment_image_src($thumbnail_id, 'medium', true);
            $object->image_medium = $image[0];

            $image = wp_get_attachment_image_src($thumbnail_id, 'large', true);
            $object->image_large = $image[0];

            $result[] = $object;
        }

        return $result;
    }

    public static function getSeafoodField($ID, $get_image = false)
    {
        $result['featured_product'] = get_post_meta($ID, 'featured_product', true);
        $result['sale_product'] = get_post_meta($ID, 'sale_product', true);
        $result['giathitruong'] = get_post_meta($ID, 'giathitruong', true);
        $result['giaban'] = get_post_meta($ID, 'giaban', true);
        $result['loai'] = get_post_meta($ID, 'loai', true);
        if ($get_image) {
            $i = 0;
            for ($x = 1; $x <= 5; $x++) {
                $img = get_post_meta($ID, 'hinh_' . $x, true);
                if ($img) {
                    $images[$i]['avatar'] = wp_get_attachment_image_src($img, 'avatar', true);
                    $images[$i]['medium'] = wp_get_attachment_image_src($img, 'medium', true);
                    $images[$i]['large'] = wp_get_attachment_image_src($img, 'large', true);
                    $i++;
                } else {
                    break;
                }
            }
            $result['images'] = $images;
        }
        return $result;
    }

}